<script>
import {
  getQuestionnaireAnalysis,
  getSexCompare,
  getVideoLove
} from '@/api/dashboard'
import * as echarts from 'echarts'

export default {
  name: 'Index',
  data() {
    return {}
  },
  created() {},
  mounted() {
    this.onGetSexCompare()
    this.onGetVideoLove()
    this.onGetQuestionnaireAnalysis()
  },
  methods: {
    onGetSexCompare() {
      getSexCompare().then((res) => {
        console.log(res)
        this.createPieSimpleRef(res.data)
      })
    },
    createPieSimpleRef(data) {
      const pieSimpleRef = echarts.init(this.$refs.pieSimpleRef)
      const option = {
        title: {
          text: '平台男女比例',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
              { value: data.man, name: '男' },
              { value: data.woman, name: '女' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      pieSimpleRef.setOption(option)
    },
    onGetVideoLove() {
      getVideoLove().then((res) => {
        console.log(res)
        this.createVideoLove(res.data)
      })
    },
    createVideoLove(data) {
      const barSimpleRef = echarts.init(this.$refs.barSimpleRef)
      const option = {
        title: {
          text: '用户最喜欢的课程',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: data.map((v) => v.title)
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data.map((v) => v.love),
            type: 'bar'
          }
        ]
      }
      barSimpleRef.setOption(option)
    },
    onGetQuestionnaireAnalysis() {
      getQuestionnaireAnalysis().then((res) => {
        console.log(res)
        this.createBarYCategory(res.data)
      })
    },
    createBarYCategory(data) {
      const barYCategoryRef = echarts.init(this.$refs.barYCategoryRef)
      const option = {
        title: {
          text: '用户填写问卷情况'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: data.map((v) => v.title)
        },
        series: [
          {
            name: '问卷',
            type: 'bar',
            data: data.map((v) => v.love)
          }
        ]
      }
      barYCategoryRef.setOption(option)
    }
  }
}
</script>

<template>
  <div class="app-container">
    <div style="display: flex; align-items: center">
      <div class="box" style="margin-right: 10px">
        <div ref="pieSimpleRef" class="simple-box" />
      </div>
      <div class="box">
        <div ref="barSimpleRef" class="simple-box" />
      </div>
    </div>
    <div class="line">
      <div ref="barYCategoryRef" class="barYCategory" />
    </div>
  </div>
</template>

<style scoped lang="scss">
.app-container {
  width: 1200px;
  margin: 0 auto;
}
.simple-box {
  width: 500px;
  height: 500px;
}
.barYCategory {
  width: 1000px;
  height: 500px;
  margin: 0 auto;
}
</style>
